package net.bingosoft.common.struts.plugin;

import java.sql.Clob;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.sql.rowset.serial.SerialClob;
import javax.sql.rowset.serial.SerialException;

import net.bingosoft.common.BeanHelper;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;


/**
 * 2007-8-3 下午10:01:52
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:页面上的数据在填充到ActionForm之前,会经过一个转换过程,此插件的目的就是为了将页面上的日期字符串自动的转换成日期类型,对于
 *        Int / Long 等数据,如果没有填入值,将其转换为null,而不使用Struts的默认转换(转换成0).
 * ChangeLog:
 */
public class DataConvertPlugIn implements PlugIn {
	private static final Log log = LogFactory.getLog(DataConvertPlugIn.class);
	/**
	 * 结束生命周期的方法
	 */
	public void destroy() {
		log.debug("重置数据转换插件...");
		ConvertUtils.deregister();
	}
	/**
	 * 数据类型转换插件注册
	 */
	public void init(ActionServlet servlet, ModuleConfig config)
	throws ServletException {
		log.debug("注册数据转换插件...");
		ConvertUtils.register(dateConverter, Date.class);
		ConvertUtils.register(stringConverter, String.class);
		ConvertUtils.register(clobConvert, Clob.class);
		ConvertUtils.register(new BigDecimalConverter(null), java.math.BigDecimal.class);
		ConvertUtils.register(new BigIntegerConverter(null), java.math.BigInteger.class);
		ConvertUtils.register(new BooleanConverter(null), java.lang.Boolean.class);
		ConvertUtils.register(new ByteConverter(null), java.lang.Byte.class);
		ConvertUtils.register(new CharacterConverter(null), java.lang.Character.class);
		ConvertUtils.register(new DoubleConverter(null), java.lang.Double.class);
		ConvertUtils.register(new FloatConverter(null), java.lang.Float.class);
		ConvertUtils.register(new IntegerConverter(null), java.lang.Integer.class);
		ConvertUtils.register(new LongConverter(null), java.lang.Long.class);
		ConvertUtils.register(new ShortConverter(null), java.lang.Short.class);
		log.debug("注册数据转换插件注册成功...");
	}
	//日期转换类
	Converter dateConverter = new Converter() {
		public Object convert(Class type, Object value) {
			if (value instanceof String && StringUtils.isNotEmpty((String)value)){
				return BeanHelper.parseDate((String) value);
			}else if(value instanceof Date){
				return value;
			}else{
				return null;
			}
		}
	};
	//字符串转换器
	Converter stringConverter = new Converter(){
		public Object convert(Class type, Object value){
			return BeanHelper.converToString(value);
		}
	};
//	大字段转换器
	Converter clobConvert = new Converter(){
		public Object convert(Class type, Object value) {
			if(value == null) return null;
			try {					
				return new SerialClob(BeanHelper.converToString(value).toCharArray()){						
					private static final long serialVersionUID = 1L;
					@Override
					public String toString() {
						String result;
						try {
							return this.getSubString(1, (int)this.length());
						} catch (SerialException e) {
							result = super.toString();
						}
						return result;
					}
				};
			} catch (SQLException e) {
				log.debug("转换大字段(Clob)失败...",e);
			}
			return null;
		}
	};
//	Converter clobConvert = new Converter(){
//		public Object convert(Class type, Object value) {
//			if(value == null) return null;
//			return Hibernate.createClob(value.toString());
//		}
//	};

}
